<!DOCTYPE html>
<html>
<head>
    <title>Mesh-Mesh Intersection</title>

    <script src="js/verb.js"></script>
    <script src="js/three.min.js"></script>
    <script src="js/verbToThreeConversion.js"></script>
    <script src="js/OrbitControls.js"></script>
    <script src="js/codemirror.js"></script>
    <script src="js/javascript.js"></script>
    <script src="js/threeBasic.js"></script>

    <link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,300' rel='stylesheet' type='text/css'>

    <link rel="stylesheet" href="css/codemirror.css">
    <link rel="stylesheet" href="css/example.css">
    <link rel="stylesheet" href="css/colorforth.css">
</head>
<body>

<div id="button">Show/Hide Code</div>
<div id="viewer" class="col">
    <div id="title"></div>
</div>
<div id="code-container" class="col scroll-y"></div>

</body>
<script id="script">

	setupScene();

	function exTorusCylindricalSurface(){

		// center, xaxis, yaxis, radius
		var profile = new verb.geom.Circle( [5,0,0], [1,0,0], [0,0,1], 2 );

		var base = [0,0,0];
		var axis = [0,0,1];
		var angle = 2 * Math.PI
		srf1 = new verb.geom.RevolvedSurface( profile, base, axis, angle );

		var axis = [-1,0,0]
			, xaxis = [0,0,1]
			, base = [8,0,0]
			, height = 16
			, radius = 2;

		srf2 = new verb.geom.CylindricalSurface( axis, xaxis, base, height, radius );

	}

	function exTorusCylindricalSurface2(){

		// center, xaxis, yaxis, radius
		var profile = new verb.geom.Circle( [5,0,0], [1,0,0], [0,0,1], 2 );

		var base = [0,0,0];
		var axis = [0,0,1];
		var angle = 2 * Math.PI
		srf1 = new verb.geom.RevolvedSurface( profile, base, axis, angle );

		var axis = [-1,0,0]
			, xaxis = [0,0,1]
			, base = [8,0,0]
			, height = 16
			, radius = 2;

		srf2 = new verb.geom.CylindricalSurface( axis, xaxis, base, height, radius );

	}

	function exTorusCylindricalSurface3(){

		// center, xaxis, yaxis, radius
		var profile = new verb.geom.Circle( [5,0,0], [1,0,0], [0,0,1], 2 );

		var base = [0,0,0];
		var axis = [0,0,1];
		var angle = 2 * Math.PI
		srf1 = new verb.geom.RevolvedSurface( profile, base, axis, angle  );

		var axis = [-1,0,0]
			, xaxis = [0,0,1]
			, base = [8,0,2]
			, height = 16
			, radius = 2;

		srf2 = new verb.geom.CylindricalSurface( axis, xaxis, base, height, radius );

	}

	function exGlancingPlaneCylindricalSurface(){

		var axis = [0,0,1]
			, xaxis = [1,0,0]
			, base = [0,0,0]
			, height = 5
			, radius = 3;

		srf1 = new verb.geom.CylindricalSurface( axis, xaxis, base, height, radius );

		var p5 	= [5,5,0]
			, p6 = [-5,5,0]
			, p7 = [-5,-5,0]
			, p8 = [5,-5,0];

		srf2 = verb.geom.NurbsSurface.byCorners( p5, p6, p7, p8 );

	}


	function exPlanePlane(){

		var p1 =   [5,0,0]
			, p2 = [5,0,6]
			, p3 = [0,1,6]
			, p4 = [0,0,0];

		srf1 = verb.geom.NurbsSurface.byCorners( p1, p2, p3, p4 );

		var p5 = [5,-5,-5]
			, p6 = [5,5,5]
			, p7 = [0,5,9]
			, p8 = [0,-5,-5];

		srf2 = verb.geom.NurbsSurface.byCorners( p5, p6, p7, p8 );

	}

	function exCylindricalSurfaceCylindricalSurface(){

		var axis = [0,0,1]
			, xaxis = [1,0,0]
			, base = [0,0,0]
			, height = 5
			, radius = 3;

		srf1 = new verb.geom.CylindricalSurface( axis, xaxis, base, height, radius );

		var axis2 = [1,0,0]
			, xaxis2 = [0,1,0]
			, base2 = [0,0,-1]
			, height2 = 10
			, radius2 = 3;

		srf2 = new verb.geom.CylindricalSurface( axis2, xaxis2, base2, height2, radius2 );

	}

	function exNurbsSurfacePlane(){

		var degree = 3
			, knots = [0, 0, 0, 0, 0.333, 0.666, 1, 1, 1, 1]
			, pts = [ 	[ [0, 0, -10], 	[10, 0, 0], 	[20, 0, 0], 	[30, 0, 0] , 	[40, 0, 0], [50, 0, 9] ],
						[ [0, -10, 0], 	[10, -10, 10], 	[20, -10, 10], 	[30, -10, 0] , [40, -10, 0], [50, -10, 0]	],
						[ [0, -20, 0], 	[10, -20, 10], 	[20, -20, 10], 	[30, -20, 0] , [40, -20, -2], [50, -20, 0] 	],
						[ [0, -30, 0], 	[10, -30, 0], 	[20, -30, 0], 	[30, -30, 0] , [40, -30, 0], [50, -30, 0]     ],
						[ [0, -40, 0], 	[10, -40, 0], 	[20, -40, 0], 	[30, -40, 4] , [40, -40, -20], [50, -40, 0]     ],
						[ [0, -50, 12], [10, -50, 0], 	[20, -50, 0], 	[30, -50, 0] , [50, -50, 0], [50, -50, 15]     ],     ];
		srf1 = new verb.geom.NurbsSurface.byKnotsControlPointsWeights( degree, degree, knots, knots, pts );

		var p5 = [50,-50,3]
			, p6 = [50,0,3]
			, p7 = [0,0,3]
			, p8 = [0,-50,5];

		srf2 = verb.geom.NurbsSurface.byCorners( p5, p6, p7, p8 );

	}

	//exTorusCylindricalSurface();
	//exGlancingPlaneCylindricalSurface();
	//exPlanePlane();
	//exPlanePlane();
	//exNurbsSurfacePlane();
	exTorusCylindricalSurface3();

	var tess1 = srf1.tessellate();
	var tess2 = srf2.tessellate();

	addMeshToScene( srf1.toThreeGeometry(), null, true );
	addMeshToScene( srf2.toThreeGeometry(), null, true );

	var res;
	var runs = 1;

	var d1 = Date.now();
	for (var ci = 0; ci < 1; ci++) {
        for (var i = 0; i < runs; i++) {
            res = verb.eval.Intersect.meshes(tess1, tess2);
        }
    }
	

	var d2 = Date.now();

	var numLengths = res.reduce(function(a,x){ return a + x.length-1; }, 0);

	console.log( "intersected in ", (d2 - d1) / runs, " ms and " + res.length + " polylines were discovered");
	console.log( numLengths + " edges were formed in total ");

	var mat = new THREE.MeshBasicMaterial({ color: 0xff0000 });
	for (var i = 0; i < res.length; i++){
		var res2 = res[i].map(function (x) { return x.point; });
		for (var j = 0; j < res2.length; j++) {
			var rp = res2[j];
            var gp = new THREE.SphereGeometry(0.005);
            addMeshToScene(gp, mat, false, rp);
        }
		addCurveToScene( pointsAsGeometry( res2 ));
	}

	renderScene();

</script>
<script src="js/example.js"></script>

</html>